package com.qing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qing.mapper.EmployeeMapper;
import com.qing.pojo.Employee;
import com.qing.pojo.SignIn;
import com.qing.mapper.SignInMapper;
import com.qing.pojo.vo.SignInVo;
import com.qing.service.EmployeeService;
import com.qing.service.SignInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qing.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qinghua
 * @since 2022-06-06
 */
@Service
public class SignInServiceImpl extends ServiceImpl<SignInMapper, SignIn> implements SignInService {

    @Autowired
    private SignInMapper signInMapper;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DateUtil dateUtil;

    @Override
    public String signIn(Integer userId) {
        Employee employee = employeeService.getOne(new QueryWrapper<Employee>().select("id").eq("user_id", userId));
        SignIn one = getOne(new QueryWrapper<SignIn>()
                .eq("employee_id", employee.getId())
                .likeRight("checkin_time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
        );
        if (one == null) {
            SignIn signIn = new SignIn();
            signIn.setEmployeeId(employee.getId());
            signIn.setCheckinTime(new Date());
            boolean b = save(signIn);
            if (b){
                return "签到成功";
            }else{
                return "签到失败";
            }
        }
        return "已经签到过了";
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String signOut(Integer userId) {
        Employee employee = employeeService.getOne(new QueryWrapper<Employee>().select("id").eq("user_id", userId));
        SignIn one = getOne(new QueryWrapper<SignIn>()
                .eq("employee_id", employee.getId())
                .likeRight("checkin_time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
        );
        if (one.getCheckoutTime() == null) {
            int check = 0;
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
                Date date = sdf.parse(String.valueOf(one.getCheckinTime()));
                if (Integer.parseInt(new SimpleDateFormat("HH").format(date)) >= 9) {
                    check = 1;
                }
                one.setCheckoutTime(new Date());
                if (Integer.parseInt(new SimpleDateFormat("HH").format(one.getCheckoutTime())) < 17) {
                    check = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            one.setDailyCheck(check);
            boolean b = updateById(one);
            if (b){
                return "签退成功";
            }else{
                return "签退失败";
            }
        }
        return "已经签退过了";
//        return update(new UpdateWrapper<SignIn>().set("checkout_time", new Date()).set("daily_check", check).eq("employee_id", employee.getId()).likeRight("checkin_time", new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
    }

    @Override
    public SignIn queryByEmployeeId(Integer userId) {
        Employee employee = employeeService.getOne(new QueryWrapper<Employee>().select("id").eq("user_id", userId));
        return getOne(new QueryWrapper<SignIn>()
                        .eq("employee_id", employee.getId())
                        .likeRight("checkin_time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
        );
    }

    @Override
    public List<SignInVo> queryAllAndName() {
        List<Map<String, Object>> workDayNum = signInMapper.getAllEmployeeWorkDayNum();
        List<SignInVo> list = signInMapper.queryAllAndName();
        Integer workDay = dateUtil.getWorkDay(new SimpleDateFormat("yyyy-MM").format(new Date()));
        for (SignInVo signIn : list) {
            signIn.setWorkDayNum(workDay);
            signIn.setMonthCheck(0);
            for (int i = 0; i < workDayNum.size(); i++) {
                if (signIn.getEmployeeId() == Integer.parseInt(String.valueOf(workDayNum.get(i).get("employeeId")))) {
                    signIn.setMonthCheck(Integer.parseInt(String.valueOf(workDayNum.get(i).get("count"))));
                    workDayNum.remove(i);
                    i = i -1;
                }
            }
        }
        return list;
    }

    @Override
    public Boolean updateSignInByEmployeeId(Integer employeeId) {
        SignIn one = getOne(new QueryWrapper<SignIn>()
                .eq("employee_id", employeeId)
                .likeRight("checkin_time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
        );

        String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        nowDate = nowDate + " " + "08:59:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        Date date = null;
        try {
            date = sdf.parse(nowDate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (one != null) {
            one.setCheckinTime(date);
            return updateById(one);
        }else {
            SignIn signIn = new SignIn();
            signIn.setEmployeeId(employeeId);
            signIn.setCheckinTime(date);
            return save(signIn);
        }
    }

    @Override
    public String updateSignOutByEmployeeId(Integer employeeId) {
        SignIn one = getOne(new QueryWrapper<SignIn>()
                .eq("employee_id", employeeId)
                .likeRight("checkin_time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()))
        );

        String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        nowDate = nowDate + " " + "17:01:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
        Date date = null;
        try {
            date = sdf.parse(nowDate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (one!= null) {
            int check = 0;
            try {
                SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
                Date date2 = sdf2.parse(String.valueOf(one.getCheckinTime()));
                if (Integer.parseInt(new SimpleDateFormat("HH").format(date2)) >= 9) {
                    check = 1;
                }
                if (Integer.parseInt(new SimpleDateFormat("HH").format(date)) < 17) {
                    check = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            one.setCheckoutTime(date);
            one.setDailyCheck(check);
            boolean b = updateById(one);
            if (b) {
                return "修改成功";
            }else{
                return "修改失败";
            }
        }else {
            return "该员工今天还没有签到";
        }

    }

}
